package com.kay;

import com.kay.Utils.GetProperties;
import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.client.ClientUtil;
import org.frameworkset.spi.BaseApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * excel import  es
 *
 * Created by Kay on 2018/11/23
 */
public class Excel2ES {
    private static Logger logger = LoggerFactory.getLogger(Excel2ES.class);
    public static void main(String[] args) throws UnsupportedEncodingException {
        Properties prop = GetProperties.GetProp();
        try {
            String filepath = prop.getProperty("filepath");
            logger.info(filepath);
            File file = new File(filepath);
            logger.info(file.getName());
            ClientExcel2DSL excel2DSL = new ClientExcel2DSL();
            Map<String,Object> excelData = excel2DSL.readExcel(file,2);
            for (String key :excelData.keySet()){
                logger.info("============= key:" + key);
                logger.info("============= value:" + excelData.get(key));
            }


            // import to es
            DirectBulkInfo( excelData);
            BaseApplicationContext.shutdown();
        }catch (Exception e){
            logger.error(e.getMessage());
        }
    }

//    // bulk basic  比較繁瑣 還要讀xml
//    public static void BulkInfo(String typeName,Map<String,Object> params){
//        ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/Client_Info.xml");
//        for (String key:params.keySet()){
//
//        }
//        Map<String,Object> inParams = new HashMap<String, Object>();
//        inParams.put("bulk",params.get("medical"));
//        clientUtil.executeHttp("client_info/" + typeName +"/_bulk","bulkImportExcelData",inParams, ClientUtil.HTTP_POST);
//
//    }

    // another direct bulk
    public static void DirectBulkInfo( Map<String,Object> params){
        ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
        for(String key:params.keySet()){
            clientUtil.executeHttp("client_info/" + key +"/_bulk",params.get(key).toString(),ClientUtil.HTTP_POST);
        }
    }
}
